package com.aurora_manito.xiaomi.Controller;


import com.aurora_manito.xiaomi.entity.Goods;
import com.aurora_manito.xiaomi.entity.PageBean;
import com.aurora_manito.xiaomi.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import java.lang.annotation.Target;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/xiaomi/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    //获取商品类型     /xiaomi/goods
    @GetMapping(value = "/getGoodsListByTypeId", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView getGoodsListByTypeId(@RequestParam String typeId, @RequestParam String currentPageNO,String pageSize)
    {
        ModelAndView modelAndView=new ModelAndView("forward:/goodsList.jsp");
        if(typeId==null)
        {
            typeId="1";
        }
        PageBean<Goods> pageBeans=new PageBean<>();
        if(currentPageNO==null)
        {
            pageBeans.setCurrentPageNO(1);
        }else{
            pageBeans.setCurrentPageNO(Integer.parseInt(currentPageNO)<1?1:Integer.parseInt(currentPageNO));
        }

        if(pageSize==null)
        {
            pageBeans.setPageSize(pageBeans.getPageSize());
        }else{
            pageBeans.setPageSize(Integer.parseInt(currentPageNO)<1?new PageBean().getPageSize():pageBeans.getPageSize());
        }

        PageBean<Goods> pageBean=goodsService.getGoodByTypeIDPageBean(typeId,pageBeans);
        modelAndView.addObject("typeId",typeId);
//        System.out.println(typeId+"and"+pageBean);
        return modelAndView.addObject("pageBean",pageBean);

//        //1.创建PageBean对象（为其各个属性赋值）
//        PageBean<Goods> pageBean=new PageBean<>();
//        //2.获取当前页号，将其赋值给PageBean对象 如果不存在设置默认页为1;
//        int pageNo=1;
//        if(!"".equals(currentPageNO)&&currentPageNO!=null)
//        {
//            pageNo=Integer.parseInt(currentPageNO);
//        }
//        pageBean.setCurrentPageNO(pageNo);
//        try{
//            pageBean=goodsService.getGoodByTypeIDPageBean(typeId,pageBean);
//            modelAndView.addObject("typeid",typeId);
//            System.out.println(pageBean);
//           return modelAndView.addObject("pageBean",pageBean);
//        }catch(Exception e) {
//            return new ModelAndView("redirect:/index.jsp");
//        }
    }
    @GetMapping(value = "/getGoodsById", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView getGoodsById(@RequestParam String id)
    {
        ModelAndView modelAndView=new ModelAndView("forward:/goodsDetail.jsp");
        Goods goods=goodsService.getGoodsById(id);
        modelAndView.addObject("goods",goods);
        System.out.println(goods);
        return modelAndView;
    }
}

